package com.boot.app.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.file.domain.FileAttachGroup;
import com.boot.app.file.service.IFileAttachGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;

@RestController
@RequestMapping("file/attach/group")
public class FileAttachGroupController {

    private final IFileAttachGroupService attachGroupService;

    @Autowired
    public FileAttachGroupController(IFileAttachGroupService attachGroupService){ this.attachGroupService = attachGroupService; }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<FileAttachGroup>> getGroupPage(@PathVariable int page, @PathVariable int size, FileAttachGroup group){
        LambdaQueryWrapper<FileAttachGroup> queryWrapper = new LambdaQueryWrapper<>();
        if(null != group.getId()) queryWrapper.eq(FileAttachGroup::getId, group.getId());
        queryWrapper.orderByDesc(FileAttachGroup::getGmtCreate);
        Page<FileAttachGroup> attachGroupPage = attachGroupService.page(new Page<>(page, size), queryWrapper);
        return ApiResult.success(attachGroupPage);
    }

    @PostMapping()
    public ApiResult<?> postGroup(@RequestBody FileAttachGroup group){
        attachGroupService.save(group);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteGroup(@PathVariable long id) throws FileNotFoundException {
        attachGroupService.removeById(id);
        return ApiResult.success();
    }

}
